package com.jt9003.controller;


import com.jt9003.pojo.User_type;
import com.jt9003.service.User_typeService;
import com.jt9003.utils.PageUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user_type")
public class User_typeController {
    @Resource
    private User_typeService user_typeService;

    @RequestMapping("/insertUserType")
    public Map<String, Object> insertUserType(String roleName, String roleExplain) {
        Map<String, Object> map = new HashMap();
        System.out.println("roleName=" + roleName + "--roleExplain=" + roleExplain);
        int i = user_typeService.insertUserType(roleName, roleExplain);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/deleteUserTypeById")
    public Map<String, Object> deleteUserTypeById(int roleId) {
        Map<String, Object> map = new HashMap();
        int i = user_typeService.deleteUserTypeById(roleId);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/updateUserTypeById")
    public Map<String, Object> updateUserTypeById(User_type user_type) {
        Map<String, Object> map = new HashMap();
        int i = user_typeService.updateUserTypeById(user_type);
        System.out.println(user_type.getRoleName() + "---" + user_type.getRoleExplain());
        if (i > 0) {
            map.put("msg", true);
            map.put("data", user_type);
        } else {
            map.put("msg", false);
        }
        System.out.println("map=" + map);
        return map;
    }

    @RequestMapping("/updateUserTypeStateById")
    public Map<String, Object> updateUserTypeStateById(User_type user_type) {
        Map<String, Object> map = new HashMap();
        int i = user_typeService.updateUserTypeStateById(user_type);
        System.out.println(i);
        if (i > 0) {
            map.put("msg", true);
            map.put("code", "0");
            map.put("data", user_type);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectUserTypeByNameOrPinyinInitials")
    public Map<String, Object> selectUserTypeByNameOrPinyinInitials(String roleName, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                    @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = user_typeService.getUserTypeCountByResult(roleName);
        List<User_type> list = user_typeService.selectUserTypeByNameOrPinyinInitials(roleName, start, limit);
        if (list != null) {
            map.put("msg", "success");
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getUserTypeCountByResult")
    public Map<String, Object> getUserTypeCountByResult(String roleName) {
        Map<String, Object> map = new HashMap();
        int i = user_typeService.getUserTypeCountByResult(roleName);
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectUserTypeList")
    public Map<String, Object> selectUsageList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                               @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = user_typeService.getUserTypeCount();
        List<User_type> list = user_typeService.selectUserTypeList(start, limit);
        if (list.size() > 0) {
            System.out.println("================start:" + start);
            map.put("msg", true);
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/deleteUserTypesById")
    public Map<String, Object> deleteUserTypesById(String checkList) {
        Map<String, Object> map = new HashMap();
        System.out.println("==>" + checkList);

        String[] strs = checkList.split(",");
        System.out.println(strs[0]);
        List<Integer> ids = new ArrayList<>();
        for (String str : strs) {
            ids.add(Integer.parseInt(str));

        }

        int i = user_typeService.deleteUserTypesById(ids);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getUserTypeCount")
    public Map<String, Object> getUserTypeCount() {
        Map<String, Object> map = new HashMap();
        int i = user_typeService.getUserTypeCount();
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }


}
